<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Credentials extends MY_Controller {

	function __construct(){
		parent::__construct();
		if(!isset($_SESSION)){
		    session_start();
		}

		$this->tab = 'user';
		$this->sub = 'borrow';

	}

	function all(){
		$data = array();
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$this->load->model('Attestation_model', 'attestation');
			$this->load->model('Attestation_type_model', 'attestation_type');
			$data = $this->get_user_info('borrow_credentials','credentials');
			

			$data['type_list'] = $this->attestation_type->get_id_names();
			$data['status_list'] =  $this->attestation->fstatus();
			$list = $this->attestation->get_list(array('user_id'=>$user['user_id']));
			$data['list'] = $list ? $list : array();
			$data['title'] = "证明材料".' - '.CON_KEYWORDS;
            $data['tab_first'] = 'borrow';
            $data['tab_second'] = 'borrow_material';
			$this->load->view('user/user_attestation_all',$data);
		}else{
			header('Location: /login.html');
		}
	}

	function upload(){
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$this->load->model('Attestation_type_model', 'attestation_type');
			$data = $this->get_user_info('borrow_upload','credentials');

			$data['type_list'] = $this->attestation_type->get_id_names();
			$data['title'] = "资料上传".' - '.CON_KEYWORDS;
            $data['tab_first'] = 'borrow';
            $data['tab_second'] = 'borrow_material';
			$this->load->view('user/user_attestation_upload',$data);
		}else{
			header('Location: /login.html');
		}
	}

	function doupload(){
		$ret = array(
			'msg' => '',
			'url' => ''
		);
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			//$ret['msg'] = json_encode($_POST);
			//echo json_encode($ret);exit;
			$this->load->model('User_model', 'user');
			$userInfo = $this->user->get_by_uid($user['user_id']);
			if ($userInfo['is_loan']==0 && $userInfo['is_self']==0 ){
				$this->load->model('Attestation_model', 'attestation');
				$num = $this->attestation->user_all($user['user_id']);
				if ($num>=20) {
					$this->js->alert('上传资料数量已超出限制');
					exit;
				}
			}

			$this->load->model('Attestation_type_model', 'attestation_type');
			$att_type = $this->attestation_type->get_one(array('type_id'=>$this->input->post('type_id')),'jifen');

			$setArr = array(
				'user_id' => $user['user_id'],
				'type_id' => $this->input->post('type_id'),
				'name'    => $this->input->post('name'),
				'fstatus'  => 0,
				'jifen'   => $att_type['jifen'],
				'content' => $this->input->post('name'),
				'add_time' => time(),
				'addip' => $this->input->ip_address(),
			);

			$path = 'static/data/upfiles/images/'.$user['user_id'].'/';
			if(!file_exists($path)){
				mkdir($path,0777);
			}
			$config['upload_path'] = $path;
			$config['allowed_types'] = 'jpg|png';
			$config['file_name'] = 'attestation_'.uniqid();
			$config['overwrite'] = true;
			$config['max_size'] = '512';
			$config['max_width']  = '2048';
			$config['max_height']  = '3000';

			$this->load->library('upload', $config);
			if($this->upload->do_upload('litpic')){
				$tmp = $this->upload->data();
				$setArr['litpic'] = 'static/data/upfiles/images/'.$user['user_id'].'/'.$config['file_name'].$tmp['file_ext'];

				//添加水印
				$this->load->library('image_lib');
				$warter['source_image'] = $setArr['litpic'];
				$warter['wm_type'] = 'overlay';
				$warter['wm_overlay_path'] = WATERMARK_IMG;//路径格式如 static/images/water.png
				$warter['wm_opacity'] = '60';
				$warter['wm_vrt_alignment'] = 'middle';
				$warter['wm_hor_alignment'] = 'center';
				$this->image_lib->initialize($warter);
				$this->image_lib->watermark();

				$this->load->model('Attestation_model', 'attestation');
				$res = $this->attestation->add($setArr);
				if($res){
					$ret['msg'] = '上传成功';
					$this->js->alert($ret['msg']);
					$this->js->go('/credentials/all/index.html');
					exit;

				}else{
					$ret['msg'] = '保存失败';
					$this->js->alert($ret['msg']);
					$this->js->go('/credentials/all/index.html');
					exit;
				}

			}else{
				$ret['msg'] = $this->upload->display_errors();
				$this->js->alert($ret['msg']);exit;
			}



		}else{
			$ret = array(
				'msg' => '请先登录',
				'url' => '/login.html'
			);
			$this->js->alert($ret['msg']);
			$this->js->go($ret['url']);
			exit;
			//echo json_encode($ret);exit;
		}
	}

	function del($id=0){
		$id = intval($id);
		$ret = array('msg'=>'错误操作','url'=>'/credentials/all.html');
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			if($id>0){
				$this->load->model('Attestation_model', 'attestation');
				$res = $this->attestation->delete(array('id'=>$id,'user_id'=>$user['user_id']));
				if($res){
					$ret = array('msg'=>'操作成功','url'=>'');
					echo json_encode($ret);exit;
				}else{
					echo json_encode($ret);exit;
				}
			}else{
				echo json_encode($ret);exit;
			}

		}else{
			$ret = array('msg'=>'请先登录','url'=>'/login.html');
			echo json_encode($ret);exit;
		}
	}

}